package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.MyMessageAdapter;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.base.BaseRcQuickAdapter;
import com.ekuaitu.kuaitu.bean.MyMessageBean;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyMessageActivity extends BaseActivity {
    @BindView(R.id.xr_message)
    XRecyclerView xr_message;
    @BindView(R.id.message_no_view)
    RelativeLayout message_no_view;
    @BindView(R.id.iv_message_return)
    ImageView iv_message_return;
    private Context mContext = this;
    private int curPage = 1;
    private MyMessageAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_message;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xr_message.setLayoutManager(linearLayoutManager);
        xr_message.setItemAnimator(new DefaultItemAnimator());
        xr_message.setEmptyView(message_no_view);
        adapter = new MyMessageAdapter(mContext, R.layout.item_my_message);
        xr_message.setPullRefreshEnabled(true);
        xr_message.setLoadingMoreEnabled(true);
        xr_message.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curPage = 1;
                loadNetworkData();
            }

            @Override
            public void onLoadMore() {
                curPage++;
                loadNetworkData();
            }
        });


        adapter.setOnItemClickListener(new BaseRcQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //通知 反馈类型
                if (adapter.getItem(position).getMessageType().equals("1") || adapter.getItem(position).getMessageType().equals("2")) {
                    Intent intent = new Intent(mContext, MesDetailActivity.class);
                    intent.putExtra("date", adapter.getItem(position).getCreateTime());
                    intent.putExtra("content", adapter.getItem(position).getContent());
                    startActivity(intent);
                    //活动类型
                } else if (adapter.getItem(position).getMessageType().equals("4")) {
                    Intent intent2 = new Intent(mContext, WebViewActivity.class);
                    intent2.putExtra(IntentKey.INTENT_WEBVIEW_H5TITLE, true);
                    intent2.putExtra(IntentKey.INTENT_WEBVIEW_URL, adapter.getItem(position).getAdditional()
                            + "?uid=" + MyApplication.getInstance().getUid()
                            + "&token=" + MyApplication.getInstance().getToken()
                            + "&platform=1"
                            + "&appVersion=" + MyApplication.getInstance().getAppVersion());
                    startActivity(intent2);
                    //有车提醒
                } else if (adapter.getItem(position).getMessageType().equals("3")) {


                }
            }
        });
        xr_message.setAdapter(adapter);
        iv_message_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        loadNetworkData();
    }


    private void loadNetworkData() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getMsg(MyApplication.getInstance().getRequestFieldMap(),curPage + "", "10").enqueue(new Callback<MyMessageBean>() {
            @Override
            public void onResponse(Call<MyMessageBean> call, Response<MyMessageBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus().equals("200")) {
                        MyMessageBean.AttachmentBean result = response.body().getAttachment();
                        if (curPage == 1) {
                            if (result != null && result.getMessageList().size() != 0) {
                                adapter.clear();
                                adapter.replaceAll(result.getMessageList());
                                xr_message.refreshComplete();
                            } else {
                                xr_message.refreshComplete();
                                xr_message.setEmptyView(message_no_view);
                            }
                        } else {
                            if (result != null && result.getMessageList().size() != 0) {
                                adapter.addAll(result.getMessageList());
                                xr_message.loadMoreComplete();
                            } else {
                                xr_message.loadMoreComplete();
                            }
                        }

                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<MyMessageBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


}
